package cd.cdyb.sms.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringUtilPlus extends StringUtils {

    public static final String PERCENT = "%";
    public static final String SLASH = "/";//back slash\
    public static final String SPACE_SLASH = " / ";
    public static final String DOT = ".";
    public static final String HYPHEN = "-";//dash
    public static final String SPACE_HYPHEN = " - ";
    public static final String UNDER_SCORE = "_";//under line
    public static final String COMMA = ",";
    public static final String COLON = ":";
    public static final String L_PT = "(";//parenthesis
    public static final String R_PT = ")";
    public static final String ENTER = "\n";
    public static final String REGEX_LAST_COMMA = ",$";

    private StringUtilPlus() {

    }


    public static String defaultString(final Number num) {
        return num == null ? EMPTY : defaultString(num.toString());
    }

    public static String leftPad(final Number num, final int size, final char padChar) {
        return leftPad(defaultString(num), size, padChar);
    }

    /**
     * 如果找到非法字符则返回true,如果没找到则返回false
     *
     * @param value 字符串
     * @return 是否含有非法字符
     */
    public static boolean stripXSS(String value) {
        boolean result = false;
        if (value != null) {
            value = value.replaceAll(" ", "");
            Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
            result = scriptPattern.matcher(value).find();
            //如果找到则为true
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }

            scriptPattern = Pattern.compile("alert", Pattern.CASE_INSENSITIVE);
            result = scriptPattern.matcher(value).find();
            if (result) {
                return true;
            }
        }
        return result;
    }

    public static CharSequence replaceEmoji(CharSequence input) {
        //emoji替换
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(input);
        if (emojiMatcher.find()) {
            return emojiMatcher.replaceAll("*");
        } else {
            return input;
        }
    }

    /**
     * 截取超过指定长度的字符串为省略号
     *
     * @param text   文本内容
     * @param length 长度
     * @return 处理结果
     */
    public static String ellipsis(String text, int length) {
        if (StringUtilPlus.isNotBlank(text) && text.length() > length) {
            return text.substring(0, length).concat("...");
        }
        return text;
    }

    /**
     * 坐标转换，百度地图坐标转换成腾讯地图坐标
     *
     * @param lat 百度坐标纬度
     * @param lon 百度坐标经度
     * @return 返回结果：纬度,经度
     */
    public static String map_bd2tx(double lat, double lon) {
        double tx_lat;
        double tx_lon;
        double x_pi = 3.14159265358979324;
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        tx_lon = z * Math.cos(theta);
        tx_lat = z * Math.sin(theta);
        return tx_lat + "," + tx_lon;
    }

    /**
     * 坐标转换，腾讯地图转换成百度地图坐标
     * * @param lat 腾讯纬度
     * @param lon 腾讯经度
     * @return 返回结果：经度,纬度
     */
    public static String map_tx2bd(double lat, double lon) {
        double bd_lat;
        double bd_lon;
        double x_pi = 3.14159265358979324;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;
        return bd_lon + "," + bd_lat;
    }



}
